package com.sky.controller.admin;

import com.sky.constant.JwtClaimsConstant;
import com.sky.context.BaseContext;
import com.sky.dto.EmployeeDTO;
import com.sky.dto.EmployeeLoginDTO;
import com.sky.dto.EmployeePageQueryDTO;
import com.sky.entity.Employee;
import com.sky.properties.JwtProperties;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.EmployeeService;
import com.sky.utils.JwtUtil;
import com.sky.vo.EmployeeLoginVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * 员工管理
 */
@RestController
@RequestMapping("/admin/employee")
@Slf4j
@Api(tags = "员工相关接口")
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;
    @Autowired
    private JwtProperties jwtProperties;


    @PutMapping
    @ApiOperation("修改员工")
    public Result update(@RequestBody EmployeeDTO employeeDTO){
        //打印
        log.info("[修改员工] employee{}",employeeDTO);
        //调用
        employeeService.updateEmp(employeeDTO);
        //响应
        return Result.success();
    }

    //根据ID查询员工(员工回显)
    @GetMapping("/{id}")
    @ApiOperation("根据ID查询员工")
    public Result<Employee> getById(@PathVariable Long id){
        //打印
        log.info("[员工回显] {}",id);
        //调用
        Employee employee = employeeService.getById(id);
        //响应
        return Result.success(employee);
    }


    //需求，启用和禁用员工
    @PostMapping("/status/{status}")
    @ApiOperation("启用和禁用员工")
    public Result getEmpStatus(@PathVariable Integer status,Long id){
        //打印
        log.info("[正在启用禁用]: {},{}",status,id);

        //调用
        employeeService.getEmpStatus(status,id);

        //响应
        return Result.success();

    }


    //需求：员工分页查询
    //接口文档：
    //写后端请求的方法:
    //员工分页查询
    //方法参数如何设计：接收前端，使用XXXDTO
    @GetMapping("page")
    @ApiOperation("员工分页查询")
    public Result<PageResult> findByPage(EmployeePageQueryDTO employeePageQueryDTO){
        //打印
        log.info("[员工分页查询] pageQuery",employeePageQueryDTO);
        //调用
        PageResult pageResult = employeeService.findByPage(employeePageQueryDTO);
        //响应
        return Result.success(pageResult);
    }



    @PostMapping
    @ApiOperation("新增员工")
    public Result addEmp(@RequestBody EmployeeDTO employeeDTO){
        log.info("[新增员工] empDto:{}",employeeDTO);
        //打印
        Long empId = BaseContext.threadLocalEmpId.get();
        String username = BaseContext.threadLocalEmpUserName.get();
        log.info("[当前线程],thread:{} empId:{},empUsername:{}",Thread.currentThread().getId(),empId,username);

        // 调用 - Service
        employeeService.addEmp(employeeDTO);
        // 响应
        return Result.success();
    }

    /**
     * 登录
     *
     * @param employeeLoginDTO
     * @return
     */
    @PostMapping("/login")
    @ApiOperation(value = "员工登录")
    public Result<EmployeeLoginVO> login(@RequestBody EmployeeLoginDTO employeeLoginDTO) {
        log.info("员工登录：{}", employeeLoginDTO);

        Employee employee = employeeService.login(employeeLoginDTO);

        //登录成功后，生成jwt令牌
        Map<String, Object> claims = new HashMap<>();
        claims.put(JwtClaimsConstant.EMP_ID, employee.getId());
        claims.put(JwtClaimsConstant.USERNAME,employee.getUsername());
        String token = JwtUtil.createJWT(
                jwtProperties.getAdminSecretKey(),
                jwtProperties.getAdminTtl(),
                claims);

        EmployeeLoginVO employeeLoginVO = EmployeeLoginVO.builder()
                .id(employee.getId())
                .userName(employee.getUsername())
                .name(employee.getName())
                .token(token)
                .build();

        return Result.success(employeeLoginVO);
    }

    /**
     * 退出
     *
     * @return
     */
    @PostMapping("/logout")
    @ApiOperation("员工退出")
    public Result<String> logout() {
        return Result.success();
    }

}
